<?php

namespace mbilling\BillingBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\RedirectResponse;

use mbilling\BillingBundle\Model\UsersBillsCurrenciesModel;
use mbilling\BillingBundle\Model\UsersAdminLogsModel;
use mbilling\BillingBundle\Entity\usersBillsCurrencies;
use mbilling\BillingBundle\Entity\usersAdminLogs;

use mbilling\BillingBundle\Form\UsersBillsCurrenciesForm;

class UsersBillsCurrenciesController extends Controller
{
    public function indexAction()
    {
        $em = $this->get('doctrine.orm.entity_manager');
        $cur_model = new UsersBillsCurrenciesModel($em);

        $curs = $cur_model->findAll();
        return $this->render('BillingBundle:UsersBillsCurrencies:index.html.twig', array('curs' => $curs));
    }
    public function addAction()
    {
        $em = $this->get('doctrine.orm.entity_manager');
        $cur_model = new UsersBillsCurrenciesModel($em);
        $cur = new usersBillsCurrencies();
        //$cur_model->create($cur->initDefaultEntity());
        //return new RedirectResponse($this->generateUrl('users_bills_currencies_show'));

        $form = $this->createForm(new UsersBillsCurrenciesForm(), $cur);

        $request = $this->get('request');
        if ($request->getMethod() == 'POST')
        {
            $form->bindRequest($request);
            if ($form->isValid())
            {
                $cur_model->create($form->getData());
                $securityContext = $this->container->get('security.context');
                $userCurrent = $securityContext->getToken()->getUser();

                $log_model = new UsersAdminLogsModel($em);
                $log = new usersAdminLogs();
                $log->setAdminId($userCurrent->getId());
                $log->setPageId('20');//страница курсов валют
                $log->setObjectId($cur->getId());//изменяемый объект
                $log->setActionTypeId('1');//создание курса валюты
                $log_model->create($log);
                return new RedirectResponse($this->generateUrl('users_bills_currencies_show'));
            }
        }
        return $this->render('BillingBundle:UsersBillsCurrencies:create.html.twig', array('form' => $form->createView()));
    }
    public function updateAction($id)
    {
        $em = $this->get('doctrine.orm.entity_manager');
        $cur_model = new UsersBillsCurrenciesModel($em);
        $cur = $cur_model->findByPK($id);

        $form = $this->createForm(new UsersBillsCurrenciesForm(), $cur);

        $request = $this->get('request');
        if ($request->getMethod() == 'POST')
        {
            $form->bindRequest($request);
            if ($form->isValid())
            {
                $cur_model->create($form->getData());
                $securityContext = $this->container->get('security.context');
                $userCurrent = $securityContext->getToken()->getUser();

                $log_model = new UsersAdminLogsModel($em);
                $log = new usersAdminLogs();
                $log->setAdminId($userCurrent->getId());
                $log->setPageId('20');//страница курсов валют
                $log->setObjectId($cur->getId());//изменяемый объект
                $log->setActionTypeId('2');//изменение курса валюты
                $log_model->create($log);
                return new RedirectResponse($this->generateUrl('users_bills_currencies_show'));
            }
        }
        return $this->render('BillingBundle:UsersBillsCurrencies:create.html.twig', array('form' => $form->createView()));
    }
    public function removeAction($id)
    {
        $em = $this->get('doctrine.orm.entity_manager');
        $cur_model = new UsersBillsCurrenciesModel($em);
        $cur_model->removeByPK($id);

        $securityContext = $this->container->get('security.context');
        $userCurrent = $securityContext->getToken()->getUser();

        $log_model = new UsersAdminLogsModel($em);
        $log = new usersAdminLogs();
        $log->setAdminId($userCurrent->getId());
        $log->setPageId('20');//страница курсов валют
        $log->setObjectId($id);//изменяемый объект
        $log->setActionTypeId('3');//удаление курса валюты
        $log_model->create($log);
        return new RedirectResponse($this->generateUrl('users_bills_currencies_show'));
    }
}